<?php


class ImageResizer extends ContentController 
{
	var $command = true;
	
	function generateFile ()
	{
		$command =$this->prefefinedFormatConvert ( $this->_getCommand() );
		
		$dimensions = explode ( 'x', $command );
		$dimensions = array_pad ( $dimensions, 2, null );
		
		$x = $dimensions[0];
		$y = $dimensions[1];
		
		if ( 
			(!$x && !$y ) 
			|| ( !is_numeric( $x ) && !is_null ( $x ))
			|| ( !is_numeric( $y )  && !is_null ( $y ) ) 
		)
		{
			$this->log ( 'null/wrong dimensions. aborting.')	;
			return false;
		}
		
		
		$this->log ( 'generateFile params: ',  get_defined_vars() );
		
		if ( !$this->Controller->configuration[ 'stopGeneration' ] )
		{
			$generated = $this->renderContent( $this->sourceFile, $this->destinationFile, $x, $y );
			$this->log ( 'generated:'.$generated );
			return $generated;
		} else {
			$this->log ( 'stopGeneration activated. Generation Stopped.');
			return false;
		}
	}
	
	
	function prefefinedFormatConvert ( $format )
	{
		if ( !isset( $_GET[ 'w' ])) $_GET[ 'w' ] = null;
		if ( !isset( $_GET[ 'h' ])) $_GET[ 'h' ] = null;
		
		$formati = array(
			'small' => array ( 'w' => 226 , 'h' => null )
			,'arts' => array ( 'w' => 226 , 'h' => null )
			,'arthome' => array ( 'w' => 80 , 'h' => 60 )
			,'imglista' => array ( 'w' => 100 , 'h' => 75 )
			,'imglisthome' => array ( 'w' => 80 , 'h' => 60 )
			,'boxp' => array ( 'w' => 138 , 'h' => 103 )
			,'boxg' => array ( 'w' => 336 , 'h' => 252 )
			,'copertina' => array ( 'w' => 87 , 'h' => null )
			,'imgdelgiorno' => array ( 'w' => 336 , 'h' => null )
			,'shotp' => array ( 'w' => 160 , 'h' => null )
			,'ngsthumb' => array ( 'w' => 156 , 'h' => null )
			,'news' => array ( 'w' => 146 , 'h' => null )
			,'poll' => array ( 'w' => 80 , 'h' => null )
			,'mini' => array (  'w' => $_GET['w'],  'h' => $_GET['h'] )
		);
		if ( isset( $formati[ $format ] ) )
		{
			$oldFormat = $formati[ $format ];
			$format = $oldFormat[ 'w' ].'x'.$oldFormat[ 'h' ];
			$this->log( 'predefined formats compatibility: '.$format.' becomes '.$format );
			
		} else {
			$this->log ( 'no predefined format conversion required' );
		}
		return $format;
	}
}

?>